package com.xingdata.jjxc.m.avt;

import jar.MessageManager;
import jar.listener.InstructionListener;
import jar.model.HeartbeatEntity;
import jar.model.RequestEntity;
import jar.tools.InstructionSet;
import jar.tools.UdpTools;

import java.io.File;

import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;

import com.alibaba.fastjson.JSON;
import com.xingdata.jjxc.R;
import com.xingdata.jjxc.base.ComBaseActivity;
import com.xingdata.jjxc.enty.RespEntity;
import com.xingdata.jjxc.http.HttpResponseListener;
import com.xingdata.jjxc.http.HttpUtil;
import com.xingdata.jjxc.utils.SDCardTools;
import com.xingdata.jjxc.utils.SystemInfo;

public class TestActivity extends ComBaseActivity implements InstructionListener,
		 OnClickListener {
	private MessageManager manager;
	int i = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.test);
//		manager = new MessageManager(SystemInfo.getServerPORT(),
//				SystemInfo.getReceivePORT(), SystemInfo.timeOut, this, this,
//				this);
//
//		TextView textView = (TextView) findViewById(R.id.tt);
//		textView.setText("设置之后");
//		System.out.println(textView.getText());
//		
//		((Button) findViewById(R.id.download)).setOnClickListener(this);
	}

	@Override
	public void onBroadcast(String arg0) {
		// TODO Auto-generated method stub
		HeartbeatEntity heartbeatEntity = new HeartbeatEntity();
		heartbeatEntity.setPhoneIP(UdpTools.getPhoneIP());
		heartbeatEntity.setModelNumber(UdpTools.getModelNumber());
		RequestEntity entity = new RequestEntity();
		entity.setMsg(JSON.toJSONString(heartbeatEntity));
		entity.setType(InstructionSet.INSTRUCTION_AUTH_TO_HUD + "");

		// i=i+1;
		// entity.setMsg(i+"");
		// entity.setType("111");
		MessageManager.send(Integer.parseInt(SystemInfo.getSendPORT()), entity);
	}

	@Override
	public void onHeartbeat(String arg0) {
		// TODO Auto-generated method stub

		HeartbeatEntity heartbeatEntity = new HeartbeatEntity();
		heartbeatEntity.setPhoneIP(UdpTools.getPhoneIP());
		heartbeatEntity.setModelNumber(UdpTools.getModelNumber());
		RequestEntity entity = new RequestEntity();
		entity.setMsg(JSON.toJSONString(heartbeatEntity));
		entity.setType(InstructionSet.INSTRUCTION_AUTH_HEARTBEAT_TO_PHONE + "");
		// i=i++;
		// entity.setMsg(i+"");
		// entity.setType("111");
		MessageManager.send(Integer.parseInt(SystemInfo.getSendPORT()), entity);
		
	}

	@Override
	public void onStateChange(int arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onCarcorder(String arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.download:
			HttpUtil.getInstance(TestActivity.this)
					.downloadFile(
							"http://192.168.31.186:9090/webserver/20151022_224711.mp4",
							 SDCardTools
								.getFilePath(SDCardTools.VIDEO_NAME)+"20151022_224711.mp4", new HttpResponseListener() {
								
								@Override
								public void onSuccess(RespEntity respEntity) {
									
									System.out.println("下载完成");
								}
								
								@Override
								public void onLoading(int vurrent) {
									System.out.println(vurrent);
								}
								
								@Override
								public void onFailure(String result) {
									System.out.println(result);
								}
							});
			break;

		default:
			break;
		}

	}

	private String getFilePath() {
		boolean sdCardExist = Environment.getExternalStorageState().equals(
				android.os.Environment.MEDIA_MOUNTED); //
		File sdDir = null;
		if (sdCardExist) {
			sdDir = Environment.getExternalStorageDirectory();//
		}
		// return sdDir.toString()+"";
		return sdDir.toString() + "/jjxc/webserver/1.apk";
	}

	@Override
	protected void initViews() {
		
	}




}
